package net.xju3_4.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.xju3_4.common.framework.simplemvc.RequestMapping;
import net.xju3_4.common.utils.JSONUtil;
import net.xju3_4.common.utils.Result;
import net.xju3_4.service.PaymentSimulationService;
import net.xju3_4.service.impl.PaymentSimulationServiceImpl;

import java.util.HashMap;
import java.util.Map;

/**
 * 支付模拟控制器
 * 独立的支付模拟功能控制器
 */
@RequestMapping("/api/payment")
public class PaymentSimulationController {

    private PaymentSimulationService paymentService = new PaymentSimulationServiceImpl();

    /**
     * 模拟支付
     */
    @RequestMapping("/simulate.do")
    public void simulatePayment(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.setCharacterEncoding("utf-8");
            
            String orderId = req.getParameter("orderId");
            String userId = req.getParameter("userId");
            String paymentType = req.getParameter("paymentType"); // 1-微信，2-支付宝
            String amount = req.getParameter("amount");
            
            // 参数验证
            if (orderId == null || userId == null || paymentType == null || amount == null) {
                JSONUtil.printByJSON(resp, Result.fail("参数不完整"));
                return;
            }
            
            // 模拟支付处理
            Map<String, Object> paymentData = new HashMap<>();
            paymentData.put("orderId", orderId);
            paymentData.put("userId", userId);
            paymentData.put("paymentType", paymentType);
            paymentData.put("amount", amount);
            
            Map<String, Object> paymentResult = paymentService.simulatePayment(paymentData);
            
            if ((Boolean) paymentResult.get("success")) {
                JSONUtil.printByJSON(resp, Result.success(paymentResult, "支付成功"));
            } else {
                JSONUtil.printByJSON(resp, Result.fail((String) paymentResult.get("message")));
            }
            
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(resp, Result.fail("系统错误：" + e.getMessage()));
        }
    }

    /**
     * 检查支付状态
     */
    @RequestMapping("/status.do")
    public void checkPaymentStatus(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.setCharacterEncoding("utf-8");
            
            String orderId = req.getParameter("orderId");
            String userId = req.getParameter("userId");
            
            if (orderId == null || userId == null) {
                JSONUtil.printByJSON(resp, Result.fail("参数不完整"));
                return;
            }
            
            Map<String, Object> paymentStatus = paymentService.getPaymentStatus(orderId, userId);
            
            if (paymentStatus != null) {
                JSONUtil.printByJSON(resp, Result.success(paymentStatus, "查询成功"));
            } else {
                JSONUtil.printByJSON(resp, Result.fail("支付记录不存在"));
            }
            
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(resp, Result.fail("系统错误：" + e.getMessage()));
        }
    }

    /**
     * 获取支付方式列表
     */
    @RequestMapping("/methods.do")
    public void getPaymentMethods(HttpServletRequest req, HttpServletResponse resp) {
        try {
            Map<String, Object> methods = paymentService.getPaymentMethods();
            JSONUtil.printByJSON(resp, Result.success(methods, "查询成功"));
            
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(resp, Result.fail("系统错误：" + e.getMessage()));
        }
    }

    /**
     * 创建支付订单
     */
    @RequestMapping("/createOrder.do")
    public void createPaymentOrder(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.setCharacterEncoding("utf-8");
            
            String rentalOrderId = req.getParameter("rentalOrderId");
            String userId = req.getParameter("userId");
            String amount = req.getParameter("amount");
            
            if (rentalOrderId == null || userId == null || amount == null) {
                JSONUtil.printByJSON(resp, Result.fail("参数不完整"));
                return;
            }
            
            Map<String, Object> orderData = new HashMap<>();
            orderData.put("rentalOrderId", rentalOrderId);
            orderData.put("userId", userId);
            orderData.put("amount", amount);
            
            Map<String, Object> paymentOrder = paymentService.createPaymentOrder(orderData);
            
            if (paymentOrder != null) {
                JSONUtil.printByJSON(resp, Result.success(paymentOrder, "支付订单创建成功"));
            } else {
                JSONUtil.printByJSON(resp, Result.fail("支付订单创建失败"));
            }
            
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(resp, Result.fail("系统错误：" + e.getMessage()));
        }
    }

    /**
     * 模拟支付回调
     */
    @RequestMapping("/callback.do")
    public void paymentCallback(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.setCharacterEncoding("utf-8");
            
            String paymentNo = req.getParameter("paymentNo");
            String status = req.getParameter("status"); // success, failed
            
            if (paymentNo == null || status == null) {
                JSONUtil.printByJSON(resp, Result.fail("参数不完整"));
                return;
            }
            
            boolean success = paymentService.handlePaymentCallback(paymentNo, status);
            
            if (success) {
                JSONUtil.printByJSON(resp, Result.success("回调处理成功"));
            } else {
                JSONUtil.printByJSON(resp, Result.fail("回调处理失败"));
            }
            
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(resp, Result.fail("系统错误：" + e.getMessage()));
        }
    }
}
